package com.zhnl.zhnl_web.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zhnl.zhnl_web.bean.BstBusTrendsPojo;
import com.zhnl.zhnl_web.bean.BstGoodsConsultImgPojo;
import com.zhnl.zhnl_web.bean.BstGoodsGategoryBean;
import com.zhnl.zhnl_web.mapper.BstGoodsCategoryMapper;
import com.zhnl.zhnl_web.service.BstGoodsCategoryService;
import com.zhnl.zhnl_web.utils.RedisUtils;
import com.zhnl.zhnl_web.utils.TreeNodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 罗兴发
 * @data 2025/7/24 9:12
 * @description：分类-业务实现层
 */
@Service
@Slf4j
public class BstGoodsCategoryServiceImpl implements BstGoodsCategoryService {

    @Resource
    BstGoodsCategoryMapper bstGoodsCategoryMapper;
    @Resource
    private RedisUtils redisUtils;

    @Override
    public String getGoodsCategory() {
        JSONObject js = new JSONObject();
        String keys = "zhnlwebgoodscategory";
        Object o = redisUtils.get(keys);
        if (null!=o){
            // json封装
            js.put("code",200);
            js.put("msg","success");
            js.put("data",o);
        }else {
            List<Map<String,Object>> l = new ArrayList<>();

            // 查询一级分类
            List<BstGoodsGategoryBean> list = bstGoodsCategoryMapper.getBstGoodsGategoryIsP0();
            // 查询pid不为0的数据
            List<BstGoodsGategoryBean> list1 = bstGoodsCategoryMapper.getBstGoodsGategoryNotP0();
            for (BstGoodsGategoryBean bgg:list) {
                Integer gId = bgg.getGId();
                Map<String,Object> map = new HashMap<>();
                //封装其子类
                List<Map<String,Object>> bgg_list = iterateBstGoodsGategory1(list1,gId);
                map.put("value",gId);
                map.put("label",bgg.getLabel());
                map.put("image",bgg.getGImage());
                map.put("xImage",bgg.getXImage());
                map.put("color",bgg.getGColor());
                map.put("children",bgg_list);
                map.put("sortOrder",bgg.getSortOrder());
                l.add(map);
            }
            //redis缓存
            redisUtils.set(keys,l,10);

            // json封装
            js.put("code",200);
            js.put("msg","success");
            js.put("data",l);
        }

        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }

    @Override
    public String getGoodsCategoryByName(JSONObject json) {
        JSONObject js = new JSONObject();

        //前端参数
        String name = json.getString("name");
        String keys = "zhnlwebgoodscategorybyname";
        Object o = redisUtils.get(keys+name);
        if (null!=o){
            // json封装
            js.put("code",200);
            js.put("msg","success");
            js.put("data",o);
        }else {

            //装载更新热度的ID集合
            List<Integer> idList = new ArrayList<>();

            //搜索出相关的分类
            List<BstGoodsGategoryBean> list = bstGoodsCategoryMapper.getGoodsCategoryByName(name);

            // 名字不为空则将热度加一
            if (!name.equals("")){
                for (BstGoodsGategoryBean b :list){
                    Integer gId = b.getGId();
                    idList.add(gId);
                }
            }
            List<BstGoodsGategoryBean> l = TreeNodeUtil.buildTree(list);
            if (idList.size()>0){
                //批量将被搜索的分类热度加一
                bstGoodsCategoryMapper.updateHotById(idList);
            }
            //redis缓存
            redisUtils.set(keys+name,l,10);

            // json封装
            js.put("code",200);
            js.put("msg","success");
            js.put("data",l);
        }

        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }

    @Override
    public String getBusinessTrends(JSONObject json) {
        JSONObject js = new JSONObject();
        // 前端传递的参数
        String title = json.getString("title");//标题
        Integer sign = json.getInteger("sign");
        Integer page = json.getInteger("page");
        Integer size = json.getInteger("size");

        // 计算初始页
        Integer startPage = (page - 1) * size;

        // redisKey
        String dataKey ="businesstrends";
        String countKey = "businesstrendscount";

        // redis查询
        Object o = redisUtils.get(countKey+title+sign+startPage+size);
        if (null!=o){
            // redis查询数据
            Object o1 = redisUtils.get(dataKey+title+sign+startPage+size);

            // json封装
            js.put("totalPage",o);
            js.put("data",o1);
            js.put("msg","success");
            js.put("code",200);
        }else {
            // 查询总条数
            Integer count = bstGoodsCategoryMapper.getBusTrendsCountByParam(title,sign);

            // 查询操作
            List<BstBusTrendsPojo> list = bstGoodsCategoryMapper.getBusTrendsByParam(title,sign,startPage,size);

            // redis缓存
            boolean set = redisUtils.set(countKey + title + sign + startPage + size, count, 2);
            boolean set1 = redisUtils.set(dataKey + title + sign + startPage + size, list, 2);

            if (!set||!set1){
                js.put("msg","fails");
                js.put("code",4016);
            }else {
                // json封装
                js.put("totalPage",count);
                js.put("data",list);
                js.put("msg","success");
                js.put("code",200);
            }
        }
        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }

    @Override
    public String getGoodsCategoryByHot() {
        JSONObject js = new JSONObject();

        //查询分类热搜前十条
        List<BstGoodsGategoryBean> list = bstGoodsCategoryMapper.getGoodsCategoryByHot();

        //json封装
        js.put("data",list);
        js.put("code",200);
        js.put("msg","success");

        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }

    @Override
    public String getGoodsCategoryList() {
        JSONObject js = new JSONObject();

        List<BstGoodsGategoryBean> list = bstGoodsCategoryMapper.getGoodsCategoryList();
        List<BstGoodsGategoryBean> list1 = TreeNodeUtil.buildTree(list);

        js.put("data",list1);
        js.put("code",200);
        js.put("msg","success");
        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }

    @Override
    public String getCategoryImgById(JSONObject json) {
        JSONObject js = new JSONObject();

        Integer gId = json.getInteger("g_id");//分类ID
        List<BstGoodsConsultImgPojo> ls = bstGoodsCategoryMapper.getGoodsGategoryImgById(gId);

        js.put("data",ls);
        js.put("code",200);
        js.put("msg","success");

        return JSON.toJSONString(js, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 商品子类规格化
     * @return
     */
    public List<Map<String,Object>> iterateBstGoodsGategory1(List<BstGoodsGategoryBean> list, Integer parentId) {
        List<Map<String,Object>> result = new ArrayList<>();
        //得到对象
        for (BstGoodsGategoryBean bgg : list) {
            Map<String,Object> map = new HashMap<>();

            //获取商品id
            Integer gId = bgg.getGId();

            //获取商品的父级id
            String parentId1 = String.valueOf(bgg.getParentId());
            if (StringUtils.isNotBlank(parentId1)) {
                if (parentId1.equals(String.valueOf(parentId))) {

                    //递归查询当前子菜单的子菜单
                    List<Map<String,Object>> iterateB = iterateBstGoodsGategory1(list, gId);
                    map.put("value",gId);
                    map.put("label",bgg.getLabel());
                    map.put("image",bgg.getGImage());
                    map.put("xImage",bgg.getXImage());
                    map.put("color",bgg.getGColor());
                    map.put("children",iterateB);
                    map.put("sortOrder",bgg.getSortOrder());
                    result.add(map);
                }
            }
        }
        return result;
    }
}
